package com.hainatong.shiphelper.module.warehouse.fragment;

import android.os.Bundle;
import android.view.View;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.App;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentShelfBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaShelfCodeBean;
import com.hainatong.shiphelper.module.warehouse.entity.ShelfUuidBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.ShelfViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;


/**
 *  货架
 */
public class ShelfFragment extends MvvmLazyFragment<FragmentShelfBinding , ShelfViewModel>
{
    private static String SELF_TAG = "ShelfFragment";

    private ShelfUuidBean shelfUuidBean ;

    public static ShelfFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( SELF_TAG , scanCode );
        ShelfFragment shelf = new ShelfFragment();
        shelf.setArguments( bundle );
        return  shelf;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_shelf;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            String selfCodeStr = args.getString(SELF_TAG , "");
            if(!StringUtils.isEmpty( selfCodeStr ))
            {
                shelfUuidBean = GsonUtil.json2Obj( selfCodeStr , ShelfUuidBean.class);
            }
        }
    }

    @Override
    protected void initView(View view)
    {
        // 扫描物资入库
        viewDataBinding.btnStartRuku.setOnClickListener( v -> {
            if( ObjectUtils.isNotEmpty( shelfUuidBean ) )
            {
                // 扫描 物资 二维码
                startScanQrcode();
            }
        } );
    }

    @Override
    protected void lazyLoad()
    {
           //根据   货架 信息
        if(ObjectUtils.isNotEmpty( shelfUuidBean ))
        {
            viewDataBinding.tvShelfChangku.setText( shelfUuidBean.getWareHouseName() );
            viewDataBinding.tvShelf.setText( shelfUuidBean.getShelfName() );
        }
    }

    public void startScanQrcode()
    {
        QrManager.getInstance().init( App.getScanQrConfigByType(2) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                LogUtils.e(TAG, "onScanSuccess: " + result.getContent());
                if( result.getType() == ScanResult.CODE_QR)
                {
                    //ToastUtils.showShort( result.getContent() );

                    //拿到物资 code   跳转 入库 详情
                    ImpaShelfCodeBean codeBean = new ImpaShelfCodeBean( shelfUuidBean.getUuid() ,result.getContent() );
                    //跳转 入库 添加商品
                    ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_TYPE_RUKU_TASK, GsonUtil.obj2json(codeBean) );
                }
                else
                {
                    ToastUtils.showShort( result.getContent() );
                }
            }
        });
    }

}
